package com.luzyluis.medicalcalendar.vista;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CalendarView;
import android.widget.ListView;
import android.widget.TextView;

import com.luzyluis.medicalcalendar.R;
import com.luzyluis.medicalcalendar.clases.Tratamiento;
import com.luzyluis.medicalcalendar.controlador.TratamientosDB;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;


public class Calendario extends ActionBarActivity {
    private CalendarView miCalendario;
    private ListView miLista;
    private List<String> items;
    private ArrayAdapter adaptadorLista;
    private TratamientosDB misTrats;
   // public static int versionBD = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendario);

        miCalendario = (CalendarView) findViewById(R.id.calendarView);
        miLista = (ListView) findViewById(R.id.listView);

        items = new ArrayList<String>();
        misTrats = new TratamientosDB(this, "TratamientosDB", null, MainActivity.versionBD);


        // db = usdbh.getWritableDatabase();

        miCalendario.setDate((new Date()).getTime());
        ArrayList<Tratamiento> aLt = misTrats.medicamentosDia((new Date()).getTime());
        if (aLt.size() > 0) {
            for (Tratamiento t : aLt) {
                items.add(t.toString());
            }
        } else
            items.add("No hay tratamientos");


        adaptadorLista  = new ArrayAdapter<String>(
                getApplicationContext(),android.R.layout.simple_list_item_1, items){

            @Override
            public View getView(int position, View convertView,
                                ViewGroup parent) {
                View view =super.getView(position, convertView, parent);

                TextView textView=(TextView) view.findViewById(android.R.id.text1);

                        /*YOUR CHOICE OF COLOR*/
                textView.setTextColor(Color.BLACK);

                return view;
            }
        };
        miLista.setAdapter(adaptadorLista);

        miCalendario.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                                            int month, int dayOfMonth) {
                items.clear();
                //Log.e("event", "datachanged");
                ArrayList<Tratamiento> aLt = misTrats.medicamentosDia(miCalendario.getDate());
                if (aLt.size() > 0) {
                    for (Tratamiento t : aLt) {
                        items.add(t.toString());
                    }
                } else
                    items.add("No hay tratamientos");

                adaptadorLista = new ArrayAdapter<String>(
                        getApplicationContext(),android.R.layout.simple_list_item_1, items){

                    @Override
                    public View getView(int position, View convertView,
                                        ViewGroup parent) {
                        View view =super.getView(position, convertView, parent);

                        TextView textView=(TextView) view.findViewById(android.R.id.text1);

                        /*YOUR CHOICE OF COLOR*/
                        textView.setTextColor(Color.BLACK);

                        return view;
                    }
                };
                miLista.setAdapter(adaptadorLista);
            }

        });

        miLista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        boolean retorno = super.onOptionsItemSelected(item);

        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_agregar:
                lanzarActivity(1);
                retorno = true;
                break;
            case R.id.action_listar:
                lanzarActivity(2);
                retorno = true;
                break;
            case R.id.action_calendario:
                lanzarActivity(3);
                retorno = true;
                break;
            case R.id.action_datos:
                lanzarActivity(4);
                retorno = true;
                break;
        }

        return retorno;


    }

    public void lanzarActivity(int key)
    {
        Intent i = null;
        switch (key)
        {
            case 1: // Agregar
                i = new Intent(this, Agregar.class);
                startActivityForResult(i, 1234);
                break;
            case 2: // Tratamientos
                i = new Intent(this, Tratamientos.class);
                startActivity(i);
                break;
            case 3: // Calendario
                i = new Intent(this, Calendario.class);
                startActivity(i);
                break;
            case 4: // Datos
                i = new Intent(this, DatosPersonales.class);
                startActivity(i);
                break;
        }
    }

}

